package com.loong.order.handler;

import com.loong.common.rest.rpc.RPCResponse;
import com.loong.common.utils.TransformUtils;
import com.loong.data.dto.ShopDTOResult;
import com.loong.data.service.IShopService;
import com.loong.order.business.PaymentOrderBusiness;
import com.loong.order.common.forehead.result.OrderBaseResult;
import com.loong.order.common.forehead.result.PaymentOrderForeheadResult;
import com.loong.order.model.PaymentOrder;
import com.loong.seller.dto.ShopTypeRPCResult;
import com.loong.seller.service.IShopTypeService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description: 支付订单服务
 * @Author: hzp
 * @Date: 2021-1-12 9:14
 */
@Component
public class PaymentOrderHandler {
    @Resource
    private PaymentOrderBusiness paymentOrderBusiness;

    @Resource
    private OrderBaseHandler orderBaseHandler;

    @DubboReference
    private IShopService iShopService;

    @DubboReference
    private IShopTypeService iShopTypeService;

    public void save(PaymentOrder paymentOrder){
        paymentOrderBusiness.save(paymentOrder);
    }

    public PaymentOrderForeheadResult findPaymentOrder(Long id) {
        OrderBaseResult orderBaseResult = orderBaseHandler.findById(id);
        PaymentOrder paymentOrder = paymentOrderBusiness.findBy("orderId",id);
        PaymentOrderForeheadResult transform = TransformUtils.transform(paymentOrder, PaymentOrderForeheadResult.class);
        RPCResponse<ShopDTOResult> shopModel = iShopService.findShopModelById(orderBaseResult.getShopId());
        Long shopTypeId = shopModel.getData().getShopTypeId();
        RPCResponse<ShopTypeRPCResult> shopType = iShopTypeService.findShopTypeById(shopTypeId);
        ShopTypeRPCResult data = shopType.getData();
        String shopTypeName = data.getShopTypeName();
        //早餐  美食   夜宵 有货到付款
        if("早餐".equals(shopTypeName)||"美食".equals(shopTypeName)||"夜宵".equals(shopTypeName)){
            transform.setFlag(true);
        }else {
            transform.setFlag(false);
        }
        return transform;
    }

    public PaymentOrder findByOrderId(Long orderId){
        PaymentOrder paymentOrder = paymentOrderBusiness.findBy("orderId",orderId);
        return paymentOrder;
    }

    public void update(PaymentOrder paymentOrder) {
        paymentOrderBusiness.updateByPrimaryKeySelective(paymentOrder);
    }
}
